

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.powerfit &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.powerfit</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">agpy.mpfit</span> <span class="kn">as</span> <span class="nn">mpfit</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<div class="viewcode-block" id="powerfit"><a class="viewcode-back" href="../../agpy.html#agpy.powerfit.powerfit">[docs]</a><span class="k">def</span> <span class="nf">powerfit</span><span class="p">(</span><span class="n">xax</span><span class="p">,</span><span class="n">data</span><span class="p">,</span><span class="n">err</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">alphaguess</span><span class="o">=-</span><span class="mf">2.0</span><span class="p">,</span><span class="n">scaleguess</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span><span class="n">quiet</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fit a power law (a line in log-space) to data as a function of x</span>
<span class="sd">    differs from &#39;plfit&#39; because plfit fits a power law distribution, </span>
<span class="sd">    this code simply fits a power law</span>
<span class="sd">    &quot;&quot;&quot;</span>
    
    <span class="n">logdata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">err</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">err</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;float&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">mpfitfun</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">err</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">fjac</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(((</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">+</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">xax</span><span class="p">)</span><span class="o">*</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">-</span><span class="n">data</span><span class="p">)</span><span class="o">/</span><span class="n">err</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">f</span>
        
    <span class="n">mp</span> <span class="o">=</span> <span class="n">mpfit</span><span class="o">.</span><span class="n">mpfit</span><span class="p">(</span><span class="n">mpfitfun</span><span class="p">(</span><span class="n">logdata</span><span class="p">,</span><span class="n">err</span><span class="p">),</span><span class="n">xall</span><span class="o">=</span><span class="p">[</span><span class="n">scaleguess</span><span class="p">,</span><span class="n">alphaguess</span><span class="p">],</span><span class="n">quiet</span><span class="o">=</span><span class="n">quiet</span><span class="p">)</span>
    <span class="n">fitp</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">params</span>

    <span class="k">return</span> <span class="n">fitp</span><span class="p">,</span><span class="n">mp</span>
</div>
<div class="viewcode-block" id="brokenpowerfit"><a class="viewcode-back" href="../../agpy.html#agpy.powerfit.brokenpowerfit">[docs]</a><span class="k">def</span> <span class="nf">brokenpowerfit</span><span class="p">(</span><span class="n">xax</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">err</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">alphaguess1</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">alphaguess2</span><span class="o">=-</span><span class="mf">2.0</span><span class="p">,</span> <span class="n">scaleguess</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
        <span class="n">breakpoint</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">quiet</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fit a broken power law (a line in log-space) to data as a function of x</span>
<span class="sd">    differs from &#39;plfit&#39; because plfit fits a power law distribution, </span>
<span class="sd">    this code simply fits a power law</span>

<span class="sd">    This is a lot more intricate than the simple power law fit, since it involves</span>
<span class="sd">    fitting two power laws with different slopes</span>

<span class="sd">    Parameters:</span>
<span class="sd">    p[0] - scale</span>
<span class="sd">    p[1] - breakpoint</span>
<span class="sd">    p[2] - power 1 (xax &lt; breakpoint)</span>
<span class="sd">    p[3] - power 2 (xax &gt;= breakpoint)</span>

<span class="sd">    There are 5 parameters (NOT 4) returned because there are two scales that are *NOT*</span>
<span class="sd">    independent</span>

<span class="sd">    returns: scale1,scale2,breakpoint,alpha1,alpha2</span>

<span class="sd">    &quot;&quot;&quot;</span>
    
    <span class="n">logdata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">err</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">err</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;float&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">brokenpowerlaw</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
        <span class="n">lowerhalf</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">xax</span><span class="p">)</span><span class="o">*</span><span class="n">p</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">*</span> <span class="p">(</span><span class="n">xax</span> <span class="o">&lt;</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="c"># find the location at which both functions must be equal</span>
        <span class="n">scale2loc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">xax</span> <span class="o">-</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
        <span class="n">scale2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">xax</span><span class="p">[</span><span class="n">scale2loc</span><span class="p">])</span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">p</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">upperhalf</span> <span class="o">=</span> <span class="p">(</span><span class="n">scale2</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">xax</span><span class="p">)</span><span class="o">*</span><span class="n">p</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span> <span class="o">*</span> <span class="p">(</span><span class="n">xax</span> <span class="o">&gt;=</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="c"># DEBUG print &quot;scale1: %15g   scale2: %15g  xaxind: %5i  xaxval: %15g  lower: %15g upper: %15g&quot; % (p[0],scale2,scale2loc,np.log10(xax[scale2loc]),lowerhalf[scale2loc-1],upperhalf[scale2loc])</span>
        <span class="k">return</span> <span class="n">lowerhalf</span><span class="o">+</span><span class="n">upperhalf</span>


    <span class="k">def</span> <span class="nf">mpfitfun</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">err</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">fjac</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">((</span><span class="n">brokenpowerlaw</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">-</span><span class="n">data</span><span class="p">)</span><span class="o">/</span><span class="n">err</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">f</span>

    <span class="k">if</span> <span class="n">breakpoint</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">breakpoint</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">xax</span><span class="p">)</span>

    <span class="n">parinfo</span> <span class="o">=</span> <span class="p">[{},</span> <span class="p">{</span><span class="s">&#39;mpminstep&#39;</span><span class="p">:</span><span class="n">xax</span><span class="o">.</span><span class="n">min</span><span class="p">(),</span><span class="s">&#39;mpmaxstep&#39;</span><span class="p">:</span><span class="n">xax</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span><span class="s">&#39;step&#39;</span><span class="p">:</span><span class="n">xax</span><span class="o">.</span><span class="n">min</span><span class="p">()},</span> <span class="p">{},</span> <span class="p">{}]</span>
        
    <span class="n">mp</span> <span class="o">=</span> <span class="n">mpfit</span><span class="o">.</span><span class="n">mpfit</span><span class="p">(</span><span class="n">mpfitfun</span><span class="p">(</span><span class="n">logdata</span><span class="p">,</span><span class="n">err</span><span class="p">),</span><span class="n">xall</span><span class="o">=</span><span class="p">[</span><span class="n">scaleguess</span><span class="p">,</span><span class="n">breakpoint</span><span class="p">,</span><span class="n">alphaguess1</span><span class="p">,</span><span class="n">alphaguess2</span><span class="p">],</span><span class="n">quiet</span><span class="o">=</span><span class="n">quiet</span><span class="p">,</span><span class="n">parinfo</span><span class="o">=</span><span class="n">parinfo</span><span class="p">)</span>
    <span class="n">fitp</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">params</span>

    <span class="n">scale2loc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">xax</span> <span class="o">-</span> <span class="n">fitp</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
    <span class="n">scale2</span> <span class="o">=</span> <span class="mi">10</span><span class="o">**</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">xax</span><span class="p">[</span><span class="n">scale2loc</span><span class="p">])</span><span class="o">*</span><span class="p">(</span><span class="n">fitp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">fitp</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">fitp</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="p">)</span>
    <span class="n">fitp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span><span class="n">fitp</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">scale2</span><span class="p">]</span> <span class="o">+</span> <span class="n">fitp</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span> <span class="p">)</span>

    <span class="k">return</span> <span class="n">fitp</span><span class="p">,</span><span class="n">mp</span></div>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>